package com.hs.framework.utils

import android.text.TextUtils
import android.view.ViewGroup.MarginLayoutParams
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import java.util.regex.Pattern

object ValidateUtils {
    fun setError(view: TextView, errorMsg: String?) {
        view.post {
            view.requestFocus()
            view.error = errorMsg
        }
    }

    //    public static void setError(WDropDownList list, String errorMsg) {
    //        list.post(() -> list.setError(errorMsg));
    //    }
    fun notNull(view: EditText, viewName: String): Boolean {
        if (TextUtils.isEmpty(view.text.toString())) {
            setError(view, viewName + "不能为空")
            return false
        }
        return true
    }


    //    public static boolean notNull(WDropDownList list, String viewName) {
    //        if (StringUtils.isEmpty(list.getSelectItem())) {
    //            setError(list, viewName + "不能为空");
    //            return false;
    //        }
    ////        setError(list, null);
    //        return true;
    //    }
    fun matchesPattern(
        view: EditText,
        viewName: String,
        pattern: String,
        errorMsg: String?
    ): Boolean {
        if (!notNull(view, viewName)) {
            return false
        }

        try {
            view.requestFocus()
            if (!Pattern.matches(pattern, view.text.toString())) setError(view, errorMsg)
        } catch (e: Exception) {
            setError(view, errorMsg)
            return false
        }

        return true
    }

    fun matchesHttpUrl(view: EditText, viewName: String, errorMsg: String?): Boolean {
        return matchesPattern(
            view,
            viewName,
            "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$",
            errorMsg
        )
    }

    fun matchesPhone(view: EditText, viewName: String, errorMsg: String?): Boolean {
        return matchesPattern(view, viewName, "^1[3-9]\\d{9}$", errorMsg)
    }

    fun matchesPassword(
        view: EditText,
        visibleIcon: ImageView,
        viewName: String,
        errorMsg: String?
    ): Boolean {
        val valid = matchesPattern(view, viewName, "^\\d{6}$", errorMsg)

        val marginRight = if (valid) {
            10
        } else {
            33
        }

        view.post {
            val params = visibleIcon.layoutParams as MarginLayoutParams
            params.rightMargin = marginRight
            visibleIcon.layoutParams = params
        }

        return valid
    }

    fun matchesIDCard(view: EditText, viewName: String, errorMsg: String?): Boolean {
        return matchesPattern(view, viewName, "(^\\d{15}$)|(^\\d{17}([0-9]|X)$)", errorMsg)
    }

    fun matchesIPv4(view: EditText, viewName: String, errorMsg: String?): Boolean {
        return matchesPattern(
            view,
            viewName,
            "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$",
            errorMsg
        )
    }

    fun matchesServiceIPv4(view: EditText, viewName: String, errorMsg: String?): Boolean {
        return matchesPattern(
            view,
            viewName,
            "^(http|https)(://)(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$",
            errorMsg
        )
    }

    fun matchesMsgIPv4(view: EditText, viewName: String, errorMsg: String?): Boolean {
        return matchesPattern(
            view,
            viewName,
            "^(tcp)(://)(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$",
            errorMsg
        )
    }
}
